package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private UserService userService;

    /**
     * 用户下单
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        // orderService.submit();
        return R.success("下单成功");
    }

    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders) {
        log.info("修改订单状态");
        Integer status = orders.getStatus();
      //  Long id = orders.getId();
        orders.setStatus(status+1);
        orderService.updateById(orders);

        return R.success("修改状态成功");
    }

    /**
     * 查看订单详情
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> userPage(int page, int pageSize) {
        Page<Orders> ordersPage = new Page(page, pageSize);

        Page<OrdersDto> ordersDtoPage = new Page<>();

        Long userId = BaseContext.getCurrentId();
        //Orders orders= orderService.getById(userId);
        LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件  用户id
        lambdaQueryWrapper.eq(Orders::getUserId, userId);
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Orders::getOrderTime).orderByAsc(Orders::getPayMethod);

        orderService.page(ordersPage, lambdaQueryWrapper);


        //此时的page内需要添加  菜品的名字 以及菜品的数量

        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");

        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Orders::getUserId, userId);
        List<Orders> orderslist = orderService.list(lqw);

        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> dtoList = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto);
            Long orderid = item.getId();
            //构建订单详情的查询条件
            LambdaQueryWrapper<OrderDetail> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(OrderDetail::getOrderId, orderid);
            List<OrderDetail> list = orderDetailService.list(lqw2);
            ordersDto.setOrderDetails(list);

            ordersDto.setUserName(userService.getById(userId).getName());
            ordersDto.setPhone(item.getPhone());
            ordersDto.setAddress(item.getAddress());
            ordersDto.setConsignee(item.getConsignee());
            return ordersDto;
        }).collect(Collectors.toList());

        ordersDtoPage.setRecords(dtoList);
        return R.success(ordersDtoPage);
    }

    /**
     * 后台查询订单详情
     *
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */

    @GetMapping("/page")
    public R<Page<OrdersDto>> page(int page, int pageSize, String number, String beginTime, String endTime) {
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        LambdaQueryWrapper<Orders> lqw = new LambdaQueryWrapper<>();
        lqw.like(number != null, Orders::getNumber, number);

        //  lqw.between(beginTime != null, Orders::getOrderTime, beginTime, endTime);
        lqw.ge(beginTime != null, Orders::getOrderTime, beginTime).
                le(endTime != null, Orders::getOrderTime, endTime);
        orderService.page(ordersPage, lqw);

        //此时ordersPage 内的数据不满足前端回显的内容   即内部的数据不完整
        //数据拷贝
        BeanUtils.copyProperties(ordersPage, ordersDtoPage, "records");
        List<Orders> records = ordersPage.getRecords();
        List<OrdersDto> dtoList = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            //基础数据的拷贝
            BeanUtils.copyProperties(item, ordersDto);
            //得到ordersid
            Long ordersid = item.getId();
            //得到用户端用户的Id
            Long userId = item.getUserId();

            //拷贝 OrdersDetail 内的内容
            LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(OrderDetail::getOrderId, ordersid);
            List<OrderDetail> orderDetailList = orderDetailService.list(lambdaQueryWrapper);
            //填充OrdersDetail 内的内容到OrdersDto中
            ordersDto.setOrderDetails(orderDetailList);
            //填充用户段的用户信息
            // ordersDto.setUserName(userService.getById(userId).getName());
            ordersDto.setPhone(item.getPhone());
            ordersDto.setAddress(item.getAddress());
            ordersDto.setConsignee(item.getConsignee());
            return ordersDto;
        }).collect(Collectors.toList());
        ordersDtoPage.setRecords(dtoList);
        // lqw.l
        return R.success(ordersDtoPage);
    }


}